<?php

function get_template($typeTpl, $params = null) {
    $html = null;
    switch ($typeTpl) {
        case 0:
            $html = get_template_menu();
            break;
        case 1:
            $html = get_menu_content();
            break;
        default:
            break;
    }
    return $html;
}

/*
 *  Get template menu
 */
function get_template_menu() {
    global $tpl, $az_db;
    //Create list user
    $pager = new Az_Pager();
    $file_tpl = 'menu/list_menu';
    $listMenu = $tpl->get_main($file_tpl, 2);
    $sql_P = "SELECT * FROM `az_category` WHERE `type`=1 or `type`=2 ORDER BY `category_id` DESC";
    $result_P = $az_db->executeSQL_Query($sql_P);
    $list_menu = '';
    if ($result_P->num_rows > 0) {
        $list_menu .= '<div class="dd" id="nestable"><ol class="dd-list">';
        $i = 0;
        while ($rows = $result_P->fetch_array(MYSQLI_ASSOC)) {
            $i++;
            $cat_name = $rows['name'];
            $cat_id = $rows['category_id'];
            $list_menu.='<li class="dd-item" data-id="' . $cat_id . '"><div class="dd-handle">' . $cat_name . '</div></li>';
        }
        $result_P->free();
        if ($i > 0) {
            $list_menu .='</ol></div>';
        } else {
            $list_menu .='<b>Sorry server is too busy please try again!</b>';
        }
        $list_option = get_all_menu();
        $list_menu = $tpl->get_vars($listMenu, array('list_menu_new' => $list_menu, 'list_menu_sys' => $list_option));
        return $list_menu;
    }
    $result_P->free();
    return $list_menu;
}

/*
 * Get all menu for 
 */
function get_all_menu() {
    global $az_db;
    $sql_P = "SELECT `id`,`option_name`,`option_type` FROM `az_option_value` WHERE `option_type`='menu' ORDER BY `id` DESC ";
    $result_P = $az_db->executeSQL_Query($sql_P);
    $option_list = '<option value="0" selected="true">--Chọn một loại menu để hiển thị--</option>';
    if ($result_P->num_rows > 0) {
        while ($rows = $result_P->fetch_array(MYSQLI_ASSOC)) {
            $option_id = $rows['id'];
            $option_name = $rows['option_name'];
            $option_list.='<option value="' . $option_id . '">' . $option_name . '</option>';
        }
        $result_P->free();
        return $option_list;
    }
    $result_P->free();
    return $option_list;
}

?>
